<?php
/**
 * @package Spider Contacts
 * @author Web-Dorado
 * @copyright (C) 2012 Web-Dorado. All rights reserved.
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 **/


defined('_JEXEC') or die('Restricted access');



$prod_iterator = 0;

$rows = $this->rows;

$option = $this->option;

$params = $this->params;

$page_num = $this->page_num;

$cont_count = $this->cont_count;

$cont_in_page = $this->cont_in_page;

$categories = $this->categories;

$category_list = $this->category_list;

$param_categories = $this->param_categories;
if ($params->get('table_radius')):
?>
<style type="text/css">
#contactMainDiv, .spidercontactbutton, .spidercontactinput  , .table_contact
{
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
}

#contactMainDiv #contTitle
{
-webkit-border-top-right-radius: 8px;
-webkit-border-top-left-radius: 8px;
-moz-border-radius-topright: 8px;
-moz-border-radius-topleft: 8px;
border-top-right-radius: 8px;
border-top-left-radius: 8px;
}
</style>
<?php
endif;
  if ($params->get("change_on_hover")):
echo '<style>
.table_contact  tr:hover  {
    background-color: '.$params->get("hover_color").';
	
}



 
.table_contact  tr:hover   #contTitle, 
.table_contact  tr:hover   span   {
    background-color: '.$params->get("hover_color").'; 
	color: '.$params->get("hover_text_color").' !important;
}



  </style>';
 endif;  
 	$cat_id = JRequest::getVar('cat_id', 0);
if (($params->get("choose_category") and !($param_categories > 0)) or $params->get("name_search"))
  {
    echo '<form action="" method="post" name="cat_form">
<input type="hidden" name="page_num"	value="1">
<div class="ContactSearchBox">';

if ($params->get("choose_category") and !($param_categories > 0))
{

	echo JText::_('SH_CHOOSE_CATEGORY') . '&nbsp;
	<select id="cat_id" name="cat_id" class="spidercontactinput" size="1" onChange="document.cat_form.submit();">
		<option value="0">' . JText::_('SH_ALL') . '</option> ';
    
    foreach ($category_list as $category)
    {
        if ($category->id == $cat_id)
            echo '<option value="' . $category->id . '"  selected="selected">' . $category->name . '</option>';
        
        else
            echo '<option value="' . $category->id . '" >' . $category->name . '</option>';
    }
        
    echo '</select>';
}

if ( $params->get("name_search"))
{
$name_search = JRequest::getVar('name_search', '');
echo '<br />
' . JText::_('SH_SEARCH') . '&nbsp;
<input id="name_search" name="name_search" class="input" value="'.$name_search.'"> 
	<input type="submit" value="'. JText::_('SH_GO') .'" class="spidercontactbutton" style="background-color:'.$params->get( 'table_button_background_color' ).'; color:'.$params->get( 'table_button_color' ).'; width:inherit;"><input type="button" value="'. JText::_('SH_RESET') .'" onClick="cat_form_reset(this.form);" class="spidercontactbutton" style="background-color:'.$params->get( 'table_button_background_color' ).'; color:'.$params->get( 'table_button_color' ).'; width:inherit;">';
}
echo '</div></form>';
}  
  

	
	  
	 
   
   
   echo ' <table class="table_contact" border="'.$params->get('table_border_width').'" style=" border-collapse:separate;  '.'border-color:' . $params->get('table_border_color') . ' !important ;border-style:' . $params->get('table_border_style_table') . '; ' . (($params->get('table_text_size_small') != '') ? ('font-size:' . $params->get('table_text_size_small') . 'px;') : '') . (($params->get('table_text_color') != '') ? ('color:' . $params->get('table_text_color') . ';') : '') . (($params->get('table_background_color') != '') ? ('background-color:' . $params->get('table_background_color') . ';') : '') . '" cellpadding="0" cellspacing="0" id="table_contact">';
     ?>
     <?php
	 if (count($rows))
	 {
	echo '<tr style="'.(($params->get('table_title_background_color') != '') ? ('background-color:' . $params->get('table_title_background_color') . ';') : '').'" align="center">';
	?>
    		<th <?php echo 'style="padding:5px; border-color:' . $params->get('table_border_color') . ' !important ;'.(($params->get('table_title_color') != '') ? ('color:' . $params->get('table_title_color') . ';') : ' ').' '.(($params->get('table_title_size_small') != '') ? ('font-size:' . $params->get('table_title_size_small') . 'px;') : ' ').'"';?> > <?php echo JText::_('SH_PICTURE') ?> </th>
    		<th <?php echo 'style=" padding:5px; border-color:' . $params->get('table_border_color') . ' !important ; '.(($params->get('table_title_color') != '') ? ('color:' . $params->get('table_title_color') . ';') : ' ').' '.(($params->get('table_title_size_small') != '') ? ('font-size:' . $params->get('table_title_size_small') . 'px;') : ' ').'"';?>> <?php echo JText::_('SH_FIRST_NAME') ?> </th>
            <th <?php echo 'style="padding:5px; border-color:' . $params->get('table_border_color') . ' !important ; '.(($params->get('table_title_color') != '') ? ('color:' . $params->get('table_title_color') . ';') : ' ').' '.(($params->get('table_title_size_small') != '') ? ('font-size:' . $params->get('table_title_size_small') . 'px;') : ' ').'"';?> > <?php echo JText::_('SH_LAST_NAME') ?></th>
            <th <?php echo 'style="padding:5px; border-color:' . $params->get('table_border_color') . ' !important ; '.(($params->get('table_title_color') != '') ? ('color:' . $params->get('table_title_color') . ';') : ' ').' '.(($params->get('table_title_size_small') != '') ? ('font-size:' . $params->get('table_title_size_small') . 'px;') : ' ').'"';?> > <?php echo JText::_('SH_CATEGORY_TABLE') ?> </th>            
            <th <?php echo 'style="padding:5px; border-color:' . $params->get('table_border_color') . ' !important ; '.(($params->get('table_title_color') != '') ? ('color:' . $params->get('table_title_color') . ';') : ' ').' '.(($params->get('table_title_size_small') != '') ? ('font-size:' . $params->get('table_title_size_small') . 'px;') : ' ').'"';?> ><?php echo JText::_('SH_EMAIL') ?> </th>
			 <th <?php echo ' width="' . $params->get('table_parameters_select_box_width').'" style="padding:5px; width:' . $params->get('table_parameters_select_box_width') . 'px; border-color:' . $params->get('table_border_color') . ' !important ; '.(($params->get('table_title_color') != '') ? ('color:' . $params->get('table_title_color') . ';') : ' ').' '.(($params->get('table_title_size_small') != '') ? ('font-size:' . $params->get('table_title_size_small') . 'px;') : ' ').'"';?> ><?php echo JText::_('SH_PARAMETERS') ?> </th>
    </tr>
    
    
  <?php  
	 
foreach ($rows as $row)
{
	
	$imgurl = explode(";", $row->image_url);


    $link = JRoute::_('index.php?option=' . $option . '&contact_id=' . $row->id . '&view=showcontact&page_num=' . $page_num . '&back=1');

	  echo '<tr style="border-color:' . $params->get('table_border_color') . ' !important ;" align="center">';
	  			echo '<td style="border-color:' . $params->get('table_border_color') . ' !important ;" id ="picture">';
		 if (!($row->image_url != "" and $row->image_url != ";"))
      {
        $imgurl[0] = "components/com_spidercontacts/images/noimage.jpg";
		  echo   '<div style="margin:5px;"><a href="'.$link.'" target="_self"><img   src="index.php?option=com_spidercontacts&view=picture&format=raw&tmpl=component&url=' . 				$imgurl[0] . '&width=' . $params->get('table_small_picture_width') . '&height=' . $params->get('table_small_picture_height') . '" /></a></div>';
        
	  }
	  
        
  else {      
			echo '<div style="margin:5px;"><a href="'.$link.'" target="_self"><img src="index.php?option=com_spidercontacts&view=picture&format=raw&tmpl=component&url=' . $imgurl[0] . '&width=' . $params->get('table_small_picture_width') . '&height=' . $params->get('table_small_picture_height') . '" /></a>

</div>';}
				echo '</td>';
				
				echo '<td style="border-color:' . $params->get('table_border_color') . ' !important ;" id = "first_name">';
				
				echo '<div id="contTitle" style=" margin:5px;font-size:' . $params->get('table_title_size_small') . 'px;' . (($params->get('table_text_color') != '') ? ('color:' . $params->get('table_text_color') . ';') : '') .'">' . $row->first_name.'</div>';
				
				
				echo '</td>';
				
					echo '<td style="border-color:' . $params->get('table_border_color') . ' !important ;" id = "last_name">';
				
				echo '<div id="contTitle" style=" margin:5px; font-size:' . $params->get('table_title_size_small') . 'px;' . (($params->get('table_text_color') != '') ? ('color:' . $params->get('table_text_color') . ';') : '') .'">' . $row->last_name.'</div>';
				
				
				echo '</td>';
				
				echo '<td style="border-color:' . $params->get('table_border_color') . ' !important ;" id="category">';
				
				 if ($row->category_id > 0)
        echo '<div style=" margin:5px;' . (($params->get('table_text_color') != '') ? ('color:' . $params->get('table_text_color') . ';') : '') . '"><span   id="cat_' . $row->id . '">' . $categories[$row->category_id] . '</span></div>';
    
    else
        echo '<span id="cat_' . $row->id . '"></span>';
				echo '</td>';
				
				echo '<td style="border-color:' . $params->get('table_border_color') . ' !important ;" id="category">';
				
				 if ($row->email != '')
        echo '<div style="margin:5px;' . (($params->get('table_text_color') != '') ? ('color:' . $params->get('table_text_color') . ';') : '') . '"><span id="email_' . $row->id . '"><a href="mailto:' .$row->email. '">' .$row->email. '</a></span></div>';
    
    else
        echo '<span id="email_' . $row->id . '"></span>';
				echo '</td>';
				
				echo '<td align="justify" style="border-color:' . $params->get('table_border_color') . ' !important ;padding-top:0; padding:0; " id="params">';
				echo '<table id="table_of_param" style=" border-collapse: separate; width:' . $params->get('table_parameters_select_box_width') . 'px;">';
					    

    $par_data = explode("par_", $row->param);
 
    for ($j = 0; $j < count($par_data); $j++)
        if ($par_data[$j] != '')
          {
            $par1_data = explode("@@:@@", $par_data[$j]);
            $par_values = explode("	", $par1_data[1]);

            $countOfPar = 0;
            
            for ($k = 0; $k < count($par_values); $k++)
                if ($par_values[$k] != "")
                    $countOfPar++;
            
            $bgcolor = ((!($j % 2)) ? (($params->get('table_params_background_color2') != '') ? ('background-color:' . $params->get('table_params_background_color2') . ';') : '') : (($params->get('table_params_background_color1') != '') ? ('background-color:' . $params->get('table_params_background_color1') . ';') : ''));
            
            
            if ($countOfPar != 0)
              {
				   $paramscolor = ((!($j % 2)) ? (($params->get('table_params_color2') != '') ? ('color:' . $params->get('table_params_color2') . ';') : '') : (($params->get('table_params_color1') != '') ? ('color:' . $params->get('table_params_color1') . ';') : ''));
				   
                echo '<tr style="' . $bgcolor . 'text-align:left"><td><b>' . $par1_data[0] . ':</b></td>';
                

                    echo '<td id="nohover" style="' . (($params->get('table_text_size_small') != '') ? ('font-size:' . $params->get('table_text_size_small') . 'px;') : '') . $bgcolor . ' '.$paramscolor.' width:' . $params->get('table_parameters_select_box_width') . 'px;"><ul class="spidercontactparamslist">';
                    
                   for ($k = 0; $k < count($par_values); $k++)
                        if ($par_values[$k] != "")
						{
							if (preg_match('/(http:\/\/[^\s]+)/', $par_values[$k], $text))
							{
							$hypertext = "<a target=\"_blank\" href=\"". $text[0] . "\">" . $text[0] . "</a>";
							$newString = preg_replace('/(http:\/\/[^\s]+)/', $hypertext, $par_values[$k]);
							echo '<li>' . $newString . '</li>';
							}
							else if (preg_match('/(https:\/\/[^\s]+)/', $par_values[$k], $text))
							{ 
								$hypertext = "<a target=\"_blank\" href=\"". $text[0] . "\">" . $text[0] . "</a>";
							$newString = preg_replace('/(https:\/\/[^\s]+)/', $hypertext, $par_values[$k]);
							echo '<li>' . $newString . '</li>';
								}
							
						 else if ($par_values[$k])
                            echo '<li>' . $par_values[$k] . '</li>';
						}
                    
                    echo '</ul></td></tr>';
                  
              }
          }
		  echo '</table>';
					
				echo '</td>';
				
			
		 echo '</tr>';
	  
  }
		
?>

</tr>
</table>
  
  <?php
  
	 }
	 
	 else
	 {echo "Nothing";}?>
            <div id="spidercontactnavigation" style="text-align:center;">

    <?php



$pos = strpos($_SERVER['QUERY_STRING'], "page_num") - 1;



if ($pos > 0)
    $url = substr($_SERVER['QUERY_STRING'], 0, $pos);

else
    $url = $_SERVER['QUERY_STRING'];





$pos = strpos($_SERVER['QUERY_STRING'], "cat_id") - 1;



if ($pos > 0)
    $url = substr($url, 0, $pos);





if ($cat_id != 0)
    $url .= "&cat_id=" . $cat_id;







if ($cont_count > $cont_in_page and $cont_in_page > 0)
  {
    $r = ceil($cont_count / $cont_in_page);
    
    
    
    
    
    $navstyle = (($params->get('table_text_size_small') != '') ? ('font-size:' . $params->get('table_text_size_small') . 'px;') : '') . (($params->get('text_color') != '') ? ('color:' . $params->get('table_text_color') . ';') : '');
    
    
    
    $link = JRoute::_('index.php?' . $url . '&page_num= ');
    
    if ($page_num > 5)
      {
        $link = JRoute::_('index.php?' . $url . '&page_num=1');
        
        echo "

&nbsp;&nbsp;<a href=\"$link\" style=\"$navstyle\">first</a>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;...&nbsp";
        
      }
	   if ($page_num > 1)
      {
        $link = JRoute::_('index.php?' . $url . '&page_num=' . ($page_num - 1));
        
        echo "&nbsp;&nbsp;<a href=\"$link\" style=\"$navstyle\">prev</a>&nbsp;&nbsp;";
        
      }
    
    
    
    for ($i = $page_num - 4; $i < ($page_num + 5); $i++)
      {
        if ($i <= $r and $i >= 1)
          {
            $link = JRoute::_('index.php?' . $url . '&page_num=' . $i);
            
            if ($i == $page_num)
                echo "<span style='font-weight:bold;color:#000000'>&nbsp;$i&nbsp;</span>";
            
            else
                echo "<a href=\"$link\" style=\"$navstyle\">&nbsp;$i&nbsp;</a>";
            
          }
        
      }
    
    
    
    
    
    if ($page_num < $r)
      {
        $link = JRoute::_('index.php?' . $url . '&page_num=' . ($page_num + 1));
        
        echo "&nbsp;&nbsp;<a href=\"$link\" style=\"$navstyle\">next</a>&nbsp;&nbsp;";
        
      }
    
    if (($r - $page_num) > 4)
      {
        $link = JRoute::_('index.php?' . $url . '&page_num=' . $r);
        
        echo "&nbsp;...&nbsp;&nbsp;&nbsp;<a href=\"$link\" style=\"$navstyle\">last</a>";
        
      }
    
  }

?></div>
  
  
  
  
  
  

  